﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Web.Routing;

using Library.Common;

namespace HanhuaMicroCredit.Admin
{
    public static class StaticHelper
    {
        /// <summary>
        /// 添加新增Form
        /// </summary>
        /// <param name="html"></param>
        /// <param name="database"></param>
        /// <param name="tableName"></param>
        /// <param name="returnUrl"></param>
        /// <returns></returns>
        public static MvcForm BeginAddForm(this HtmlHelper html, Database database, string tableName, string returnUrl)
        {
            return html.BeginForm("Add", "OperateTotal", new { dtnum = (int)database, name = DencryptHelper.Encrypt(tableName), url = returnUrl }, FormMethod.Post, new { @id = "subForm", @name = "subForm", @class = "pageForm required-validate", onsubmit = "return validateCallback(this,navTabAjaxDone)" });
        }
        public static MvcForm BeginAddMoreForm(this HtmlHelper html, Database database, string tableName, string returnUrl)
        {
            return html.BeginForm("AddMore", "OperateTotal", new { dtnum = (int)database, name = DencryptHelper.Encrypt(tableName), url = returnUrl }, FormMethod.Post, new { @id = "subForm", @name = "subForm", @class = "pageForm required-validate", onsubmit = "return validateCallback(this,navTabAjaxDone)" });
        }

        public static MvcForm BeginAddForm(this HtmlHelper html, Database database, string tableName, string returnUrl, string more)
        {
            return html.BeginForm(more, "OperateTotal", new { dtnum = (int)database, name = DencryptHelper.Encrypt(tableName), url = returnUrl }, FormMethod.Post, new { @id = "subForm", @name = "subForm", @class = "pageForm required-validate", onsubmit = "return validateCallback(this,navTabAjaxDone)" });
        }
        public static MvcForm BeginAddForm(this HtmlHelper html, Database database, string tableName, string returnUrl, string more, string controler)
        {
            return html.BeginForm(more, controler, new { dtnum = (int)database, name = DencryptHelper.Encrypt(tableName), url = returnUrl }, FormMethod.Post, new { @id = "subForm", @name = "subForm", @class = "pageForm required-validate", onsubmit = "return validateCallback(this,navTabAjaxDone)" });
        }
        /// <summary>
        /// 添加新增Form
        /// </summary>
        /// <param name="html"></param>
        /// <param name="tableName"></param>
        /// <param name="returnUrl"></param>
        /// <returns></returns>
        public static MvcForm BeginAddForm(this HtmlHelper html, string tableName, string returnUrl)
        {
            return BeginAddForm(html, Database.Default, tableName, returnUrl);
        }
        public static MvcForm BeginAddMoreForm(this HtmlHelper html, string tableName, string returnUrl)
        {
            return BeginAddMoreForm(html, Database.Default, tableName, returnUrl);
        }

        public static MvcForm BeginAddForm(this HtmlHelper html, string tableName, string returnUrl, string more)
        {
            return BeginAddForm(html, Database.Default, tableName, returnUrl, more);
        }
        public static MvcForm BeginAddForm(this HtmlHelper html, string tableName, string returnUrl, string more,string controler)
        {
            return html.BeginForm(more, controler, new { dtnum = (int)Database.Default, name = DencryptHelper.Encrypt(tableName), url = returnUrl }, FormMethod.Post, new { @id = "subForm", @name = "subForm", @class = "pageForm required-validate", onsubmit = "return validateCallback(this,navTabAjaxDone)" });
        }
        /// <summary>
        /// 添加编辑Form
        /// </summary>
        /// <param name="html"></param>
        /// <param name="database"></param>
        /// <param name="tableName"></param>
        /// <param name="id"></param>
        /// <param name="returnUrl"></param>
        /// <returns></returns>
        public static MvcForm BeginEditForm(this HtmlHelper html, Database database, string tableName, int id, string returnUrl)
        {
            return html.BeginForm("Edit", "OperateTotal", new { id = id, dtnum = (int)database, name = DencryptHelper.Encrypt(tableName), url = returnUrl }, FormMethod.Post, new { @id = "subForm", @class = "pageForm required-validate", onsubmit = "return validateCallback(this,navTabAjaxDone)" });
        }

        public static MvcForm BeginEditForm(this HtmlHelper html, Database database, string tableName, int id, string returnUrl,string more)
        {
            return html.BeginForm(more, "OperateTotal", new { id = id, dtnum = (int)database, name = DencryptHelper.Encrypt(tableName), url = returnUrl }, FormMethod.Post, new { @id = "subForm", @class = "pageForm required-validate", onsubmit = "return validateCallback(this,navTabAjaxDone)" });
        }
        public static MvcForm BeginEditForm(this HtmlHelper html, Database database, string tableName, int id, string returnUrl,int toMore)
        {
            return html.BeginForm("Edit", "OperateTotal", new { id = id, dtnum = (int)database, name = DencryptHelper.Encrypt(tableName), url = returnUrl, toMore = toMore }, FormMethod.Post, new { @id = "subForm", @class = "pageForm required-validate", onsubmit = "return validateCallback(this,navTabAjaxDone)" });
        }

        #region tn_2012_2_9
        public static MvcForm BeginEditForm(this HtmlHelper html, Database database, string tableName, int id, string returnUrl, string more, string file)
        {
            return html.BeginForm(more, file, new { id = id, dtnum = (int)database, name = DencryptHelper.Encrypt(tableName), url = returnUrl }, FormMethod.Post, new { @id = "subForm", @class = "pageForm required-validate", onsubmit = "return validateCallback(this,navTabAjaxDone)" });
        }
        #endregion
        /// <summary>
        /// 添加编辑Form
        /// </summary>
        /// <param name="html"></param>
        /// <param name="id"></param>
        /// <param name="tableName"></param>
        /// <param name="returnUrl"></param>
        /// <returns></returns>
        public static MvcForm BeginEditForm(this HtmlHelper html, int id, string tableName, string returnUrl)
        {
            return BeginEditForm(html, Database.Default, tableName, id, returnUrl);
        }
        public static MvcForm BeginEditForm(this HtmlHelper html, int id, string tableName, string returnUrl,string more)
        {
            return BeginEditForm(html, Database.Default, tableName, id, returnUrl, more);
        }
        public static MvcForm BeginEditForm(this HtmlHelper html, int id, string tableName, string returnUrl, int toMore)
        {
            return BeginEditForm(html, Database.Default, tableName, id, returnUrl, toMore);
        }
        /// <summary>
        /// 将枚举类型转换为下拉列表项
        /// </summary>
        /// <typeparam name="enumType">默认选中的枚举值</typeparam>
        /// <returns></returns>
        public static IList<SelectListItem> ConvertEnumToSelectListItem(Enum enumType)
        {
            Type t = enumType.GetType();
            IList<SelectListItem> items = new List<SelectListItem>();
            Array a = Enum.GetValues(t);
            foreach (int v in a)
            {
                items.Add(new SelectListItem() { Value = v.ToString(), Text = Enum.GetName(t, v) });
            }
            items.Single(s => int.Parse(s.Value) == Convert.ToInt32(enumType)).Selected = true;

            return items;
        }

        /// <summary>
        /// 获取Model的值，如果无该键或model为null，则返回null
        /// </summary>
        /// <param name="model"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static object GetModelValue(Dictionary<string, object> model, string key)
        {
            if (model == null)
            {
                return null;
            }

            if (!model.ContainsKey(key))
            {
                return null;
            }

            return model[key];
        }
    }
}